<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="类型" prop="typeName">
        <el-input
          v-model="queryParams.typeName"
          placeholder="请输入类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址" prop="addressName">
        <el-input
          v-model="queryParams.addressName"
          placeholder="请输入地址名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['module:clockedtype:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['module:clockedtype:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['module:clockedtype:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['module:clockedtype:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="clockedtypeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="类型" align="center" prop="typeName" v-if="columns[0].visible"/>
      <el-table-column label="地址" align="center" prop="addressName" v-if="columns[1].visible"/>
      <el-table-column label="经度" align="center" prop="longitude" v-if="columns[2].visible"/>
      <el-table-column label="纬度" align="center" prop="latitude" v-if="columns[3].visible"/>
      <el-table-column label="打卡范围" align="center" prop="scope" v-if="columns[4].visible"/>
      <el-table-column label="备注" align="center" prop="remark" v-if="columns[5].visible"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['module:clockedtype:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['module:clockedtype:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改打卡模型对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="类型" prop="typeName">
          <el-input v-model="form.typeName" placeholder="请输入类型名称" style="width:300px;" />
        </el-form-item>
        <el-form-item label="起始时间"  >
          <el-time-picker
            is-range
            v-model="form.time"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            placeholder="选择时间范围"
          >
          </el-time-picker>
          <el-button  type="primary" @click="addTime" style="margin-left: 20px">新增</el-button>
        </el-form-item>
        <div v-for="(item,index) in form.scopeList " :key="index" style="display: flex">
          <el-descriptions  class="margin-top" :column="4"  border >

            <el-descriptions-item :labelStyle="{'width':'100px'}">
              <template slot="label">
                <i class="el-icon-user"></i>
                起始时间
              </template>
              {{item.startTime}}
            </el-descriptions-item>
            <el-descriptions-item :labelStyle="{'width':'100px'}">
              <template slot="label" >
                <i class="el-icon-mobile-phone"></i>
                结束时间
              </template>
              {{item.endTime}}
            </el-descriptions-item>
          </el-descriptions>
          <div  @click="delTime(index)" class="button">删除</div>
        </div>
        <el-form-item label="地址" prop="addressName">
          <el-input placeholder="请输入地址" v-model="form.addressName" style="width:300px;">
<!--            <template slot="append" ></template>-->
            <el-button slot="append" @click="selectFn" >选择地址</el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入经度" style="width:300px;" />
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入纬度" style="width:300px;" />
        </el-form-item>
        <el-form-item label="打卡范围" prop="scope">
          <el-input v-model="form.scope" placeholder="请输入打卡范围" style="width:300px;" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" style="width:300px;" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="地图" :visible.sync="mapOpen" width="80%" append-to-body>
        <Map @mapBtn="mapBtn"></Map>
    </el-dialog>

  </div>
</template>

<script>
import { listClockedtype, getClockedtype, delClockedtype, addClockedtype, updateClockedtype } from "@/api/module/clockedtype";
import  {getDicts} from '@/api/system/dict/data'
import Map from '@/views/module/map/map'
export default {
  name: "Clockedtype",
  components:{
    Map
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 打卡模型表格数据
      clockedtypeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      mapOpen:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        typeName: null,
        clockIds: null,
        addressName: null,
        longitude: null,
        latitude: null,
        scope: null,

      },
      clocktypeoptions:[],
      // 表单参数
      count:0,
      form: {
        time:'',
        typeName:null,
        scopeList:[],
        longitude:'',
        latitude:'',
        addressName:''
      },
      columns: [
        { key: 0, label: `类型名称`, visible: true },
        { key: 1, label: `地址名称`, visible: true },
        { key: 2, label: `地址经度`, visible: true },
        { key: 3, label: `地址纬度`, visible: true },
        { key: 4, label: `允许打卡范围`, visible: true },
        { key: 5, label: `备注`, visible: true },

      ],
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getClockType()

  },
  methods: {
    //跳转到地图
    selectFn(){
      this.mapOpen=true
    },
    mapBtn(value){
      console.log(value)
      this.form.longitude=value.longitude
      this.form.latitude=value.latitude
      this.form.addressName=value.address
      this.mapOpen=value.mapOpen
    },
    addTime(){
      if(!this.form.time){
        this.$message('请选择时间！')
        return  false
      }
      // if(this.count==4){
      //   this.$message('不允许添加！')
      //   return false
      // }
      this.form.scopeList.push({
        startTime:this.getTime(this.form.time[0]),
        endTime:this.getTime(this.form.time[1]),
      })
      this.form.time=null

    },
    delTime(index){
      this.form.scopeList.splice(index,1)
    },
    //时间转换
    getTime(val){
      let  d=new Date(val)
      let  h=d.getHours()
      let  m= d.getMinutes()
      let s=d.getSeconds()
      if(h>=0 && h<10){
        h="0"+h
      }
      if(m>=0 && m<10){
        m="0"+m
      }
      if(s>=0 && s<10){
        s="0"+s
      }
      d=  h+ ':' + m + ':' + s;
      console.log(d)
      return d
    },

    /** 获取打卡类型clock_type */
    getClockType(){
      getDicts('clock_type').then(res=>{
        console.log(res)
        this.clocktypeoptions=res.data
      })
    },
    /** 查询打卡模型列表 */
    getList() {
      this.loading = true;
      listClockedtype(this.queryParams).then(response => {
        this.clockedtypeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.count=0
      this.time=[
        {
          startTime:'',
          endTime:'',
        }
      ]
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        typeName: null,
        clockIds: null,
        addressName: null,
        longitude: null,
        latitude: null,
        scope: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.reset();
      this.open = true;
      this.title = "添加打卡模型";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();

      const id = row.id || this.ids
      getClockedtype(id).then(response => {
        this.form = response.data;
        if(this.form.scopeList==null){
          this.form.scopeList=[]
        }
        this.time = response.data.scopeList;
        this.open = true;
        this.title = "修改打卡模型";
        console.log(this.count)
      });
    },
    /** 提交按钮 */
    submitForm() {
      console.log(this.form)
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateClockedtype(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addClockedtype(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除打卡模型编号为"' + ids + '"的数据项？').then(function() {
        return delClockedtype(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('module/clockedtype/export', {
        ...this.queryParams
      }, `clockedtype_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.margin-top{
  padding: 10px;
  width: 80%;
}

.button{
  width: 50px;
  height: 40px;
  line-height: 40px;
  margin-top: 10px;
  text-align: center;
  color: #fff;
  font-weight: 600;
  border-radius: 8px;
  background-color:#1890ff;
}
</style>
